package com.syn.vr.vm;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.gaoyi.basemodule.apiresult.ApiResultData;
import com.gaoyi.basemodule.baseCallback.OnResultListener;
import com.gaoyi.basemodule.baseUtil.ToastUtil;
import com.gaoyi.basemodule.basevm.BaseViewModel;
import com.google.gson.reflect.TypeToken;
import com.syn.vr.R;
import com.syn.vr.bean.CourseDto;
import com.syn.vr.bean.ShortDto;
import com.syn.vr.bean.dto.WorkerDto;
import com.syn.vr.constant.ApiStatus;
import com.syn.vr.db.entity.Record;
import com.syn.vr.repository.ComboRepository;
import com.syn.vr.repository.TagRepository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class MainViewModel extends BaseViewModel {

    private final TagRepository mRepository;
    private final ComboRepository mCombo;
    MutableLiveData<List<ShortDto>> mSiteList;
    MutableLiveData<List<CourseDto>> mCourseList;
    MutableLiveData<WorkerDto> mWorker;

    public MutableLiveData<List<ShortDto>> getSiteList() {
        if (mSiteList == null) {
            mSiteList = new MutableLiveData<>();
        }
        return mSiteList;
    }

    public MutableLiveData<List<CourseDto>> getCourseList() {
        if (mCourseList == null) {
            mCourseList = new MutableLiveData<>();
        }
        return mCourseList;
    }

    public MutableLiveData<WorkerDto> getWorker() {
        if (mWorker == null) {
            mWorker = new MutableLiveData<>();
        }
        return mWorker;
    }

    public MainViewModel(@NonNull Application application) {
        super(application);
        mRepository = new TagRepository();
        mCombo = new ComboRepository();
    }

    public void upload(String cardNo, String workName, String projId, String courseId, String courseName, Date dateTime,String WorkerId) {
        Record bean = new Record();
        bean.setCardNumber(cardNo);
        bean.setWorkName(workName);
        bean.setProjId(projId);
        bean.setCourseId(courseId);
        bean.setCourseName(courseName);
        bean.setWorkerId(WorkerId);
        bean.setCreatedAt(new Date());
        bean.setDateTime(dateTime);
        mRepository.add(bean, new OnResultListener() {
            @Override
            public void onSuccess(Object apiResult) {
                if (getView() != null) {
                    getView().hideLoading();
                }
                ApiResultData<String> apiResultData = (ApiResultData<String>) apiResult;
                mResultCode.setValue(apiResultData.getStatus());
                if (apiResultData.getStatus() != ApiStatus.SUCCESS) {
                    ToastUtil.showToast(apiResultData.getMessage());
                }
                bean.setUpload(1);
                mRepository.insert(bean);
            }

            @Override
            public void onFails(Exception e) {
                if (getView() != null) {
                    getView().hideLoading();
                }
                mResultCode.setValue(ApiStatus.SUCCESS_SAVE_LOCAL);
                mRepository.insert(bean);
            }
        }, new TypeToken<ApiResultData<String>>() {
        }.getType());
    }

    public void getSites() {
        if (getView() != null) {
            getView().showLoading();
        }
        mCombo.combo("projs", new OnResultListener() {
            @Override
            public void onSuccess(Object apiResult) {
                if (getView() != null) {
                    getView().hideLoading();
                }
                ApiResultData<List<ShortDto>> apiResultData = (ApiResultData<List<ShortDto>>) apiResult;
                if (apiResultData.getStatus() != ApiStatus.SUCCESS) {
                    ToastUtil.showToast(apiResultData.getMessage());
                    return;
                }
                mSiteList.setValue(apiResultData.getData());
            }

            @Override
            public void onFails(Exception e) {
                if (getView() != null) {
                    getView().hideLoading();
                }
                ToastUtil.showToast(R.string.network_error);
            }
        }, new TypeToken<ApiResultData<List<ShortDto>>>() {
        }.getType());
    }

    public void getCourses() {
        if (getView() != null) {
            getView().showLoading();
        }
        mRepository.getCourses(new OnResultListener() {
            @Override
            public void onSuccess(Object apiResult) {
                if (getView() != null) {
                    getView().hideLoading();
                }
                ApiResultData<List<CourseDto>> apiResultData = (ApiResultData<List<CourseDto>>) apiResult;
                if (apiResultData.getStatus() != ApiStatus.SUCCESS) {
                    ToastUtil.showToast(apiResultData.getMessage());
                    return;
                }
                mCourseList.setValue(apiResultData.getData());
            }

            @Override
            public void onFails(Exception e) {
                if (getView() != null) {
                    getView().hideLoading();
                }
                ToastUtil.showToast(R.string.network_error);
            }
        }, new TypeToken<ApiResultData<List<CourseDto>>>() {
        }.getType());
    }

    public void getWorker(String cardNo) {
        if (getView() != null) {
            getView().showLoading();
        }
        mRepository.getWorker(cardNo, new OnResultListener() {
            @Override
            public void onSuccess(Object apiResult) {
                if (getView() != null) {
                    getView().hideLoading();
                }
                ApiResultData<WorkerDto> apiResultData = (ApiResultData<WorkerDto>) apiResult;
                if (apiResultData.getStatus() != ApiStatus.SUCCESS) {
                    return;
                }
                mWorker.setValue(apiResultData.getData());
            }

            @Override
            public void onFails(Exception e) {
                if (getView() != null) {
                    getView().hideLoading();
                }
            }
        }, new TypeToken<ApiResultData<WorkerDto>>() {
        }.getType());
    }
}
